﻿// Copyright 2006 - Ricardo Stuven (rstuven@gmail.com)
//
// This file is part of MsSqlSpatial.
// MsSqlSpatial is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// MsSqlSpatial is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with MsSqlSpatial; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System.Data.SqlTypes;
using GeoAPI.Geometries;
using GisSharpBlog.NetTopologySuite.Geometries;
using Microsoft.SqlServer.Server;
using MsSqlSpatial;

public partial class UserDefinedFunctions
{
    /// <summary>  
    /// Returns this <c>Geometry</c>s bounding box. If this <c>Geometry</c>
    /// is the empty point, returns an empty <c>Point</c>. If the <c>Geometry</c>
    /// is a point, returns a non-empty <c>Point</c>. Otherwise, returns a
    /// <c>Polygon</c> whose points are (minx, miny), (maxx, miny), (maxx,
    /// maxy), (minx, maxy), (minx, miny).
    /// </summary>
    /// <param name="X1"></param>
    /// <param name="Y1"></param>
    /// <param name="X2"></param>
    /// <param name="Y2"></param>
    /// <param name="srid"></param>
    /// <returns>    
    /// An empty <c>Point</c> (for empty <c>Geometry</c>s), a
    /// <c>Point</c> (for <c>Point</c>s) or a <c>Polygon</c>
    /// (in all other cases).
    /// </returns>
    /// <remarks>
    /// </remarks>
    [SqlFunction(IsDeterministic = true)]
    public static SqlBytes MakeEnvelope(SqlDouble X1, SqlDouble Y1, SqlDouble X2, SqlDouble Y2, SqlInt32 srid)
    {
        IGeometry envelope = GeometryFactory.Default.ToGeometry(new Envelope(X1.Value, X2.Value, Y1.Value, Y2.Value));
        return ConvertGeometry.ToSqlBytes(envelope, srid.Value);
    }
} ;